package com.controller;

import com.api.CommonResult;
import com.entity.Login;
import com.entity.table.LoginTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin/user")
public class DUserController {
    @Autowired
    LoginService loginService;

    @GetMapping(value = "/userList")
    public String userList(ModelMap model, @RequestParam(value = "username", defaultValue = "")String username,
                           @RequestParam(value = "p",defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        //按照用户名模糊查找
        QueryWrapper queryWrapper = QueryWrapper.create().where(LoginTableDef.LOGIN.PWD.like(username));
        Page<Login> page = new Page<>();
        page.setPageSize(pageSize); //设置每页显示记录数
        page.setPageNumber(p);  //设置页码
        Page<Login> pageInfo = loginService.page(page, queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total",pageInfo.getTotalRow());
        return "back/user-list";
    }
    //添加会员页面代码
    @GetMapping(value = "/addView")
    public String addUserView() {
        return "back/user-add";
    }

    /**
     * 修改添加用户表代码。
     *
     * @param login 用户表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Login login){
        try {
            boolean r = loginService.save(login);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = login.getUsername()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    //添加修改管理员页面代码
    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model){
        Login login1 = loginService.getById(id);
        model.addAttribute("login", login1);
        return "back/user-edit";
    }

    /**
     * 修改根据主键更新用户表代码。
     *
     * @param login 用户表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */


    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Login login){
        try {
            boolean r = loginService.updateById(login);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = login.getUsername()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    //添加删除用户代码
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try {
            boolean r = loginService.removeById(id);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}
